<?php

class CongressesController extends AppController
{

    function coordinator_index()
    {
        $this->layout = 'coordinator_default';
        $this->Congress->recursive = 0;
        $this->set('congresses', $this->paginate());
    }

    function coordinator_view($id = null)
    {
        $this->layout = 'coordinator_default';
        if (!$id) {
            $this->Session->setFlash(__('Congresso inexistente.', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('congress', $this->Congress->read(null, $id));
    }

    function coordinator_add()
    {
        $this->layout = 'coordinator_default';
        if (!empty($this->data)) {
            $this->Congress->create();
            if ($this->Congress->save($this->data)) {
                $this->Session->setFlash(__('Congresso adicionado com sucesso.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Erro ao adicionar o Congresso.', true));
            }
        }
        $this->set('areas', $this->Congress->Area->find('list', array('order' => 'Area.name ASC')));
        $themes = $this->Congress->Theme->find('list');
        $this->set(compact('themes'));
    }

    function coordinator_edit($id = null)
    {
        $this->layout = 'coordinator_default';
        $this->User->recursive = 1;
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Congresso Inválido.', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Congress->save($this->data)) {
                $this->Session->setFlash(__('O Congresso foi salvo.', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Erro ao salvar o Congresso.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Congress->read(null, $id);
        }
        $this->set('areas', $this->Congress->Area->find('list', array('order' => 'Area.name ASC')));
        $themes = $this->Congress->Theme->find('list');
        $this->set(compact('themes'));
    }

    function coordinator_delete($id)
    {
        if (!$id) {
            $this->Session->setFlash(__('Congresso inexistente.', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Congress->delete($id)) {
            $this->Session->setFlash(__('O Congresso foi excluído', true));
            $this->redirect(array('action' => 'index'));
        }
    }

    function author_view($id)
    {
        $this->layout = 'author_default';
        $this->set('congress', $this->Congress->read(array('name', 'deadline_submission'), $id));
        $this->set('articles', $this->Congress->Article->find('all', array(
                    'conditions' => array(
                        'user_id' => $this->Session->read('Auth.User.id'),
                        'congress_id' => $id
                    )
                )));
    }

    function author_subscribe($congressId = null, $subscribe = null)
    {
        $this->layout = 'author_default';
        $this->set('pathContent', array('item1' => array('link' => '/author/users/profile', 'name' => 'Meus Congressos')));
        $verificaCongresso = $this->Congress->read(null, $congressId);
        if (empty($congressId) || empty($verificaCongresso)) {
            $this->Session->setFlash(__('Congresso inexistente.', true));
            $this->redirect(array('author' => true, 'controller' => 'users', 'action' => 'profile'));
        } else {
            if ($subscribe) {
                $userId = $this->Session->read('Auth.User.id');
                $this->Congress->query("INSERT INTO `congresses_users` (`congress_id`, `user_id`) VALUES ('$congressId', '$userId');");
                $this->Session->setFlash(__('Sua inscrição foi realizada com sucesso', true));
                $this->redirect(array('author' => true, 'controller' => 'users', 'action' => 'profile'));
            } else {
                $this->set('congress', $verificaCongresso);
            }
        }
    }

    function author_unsubscribe($congressId)
    {
        $userId = $this->Session->read('Auth.User.id');
        $congress = $this->Congress->read(null, $congressId);
        if (strtotime($congress['Congress']['deadline_submission']) <= time()) {
            $this->Session->setFlash(__('Congresso em andamento. Não é possível cancelar a inscrição', true));
            $this->redirect(array('author' => true, 'controller' => 'users', 'action' => 'profile'));
        } else {
            if ($this->Congress->query('DELETE FROM `congresses_users` WHERE `congresses_users`.`congress_id` = ' . '\'' . $congressId . '\'' . ' AND `congresses_users`.`user_id` = ' . $userId)) {
                $this->Session->setFlash(__('Sua inscrição foi cancelada', true));
                $this->redirect(array('author' => true, 'controller' => 'users', 'action' => 'profile'));
            }
        }
    }

    function coordinator_finalizeEvaluation($congressId)
    {
        $congress = $this->Congress->read(null, $congressId);
        if ((date('Y-m-d', strtotime($congress['Congress']['deadline_evaluation'])) < date('Y-m-d')) && ($congress['Congress']['status'] == 'S')):
            $this->redirect(array('controller' => 'articles', 'action' => 'finalizeArticleEvaluation', $congressId));
        endif;
    }

    function coordinator_finalizeSubmission($congressId)
    {
        $congress = $this->Congress->read(null, $congressId);
        if ((date('Y-m-d', strtotime($congress['Congress']['deadline_submission'])) < date('Y-m-d')) && ($congress['Congress']['status'] == 'A')):
            if ($this->Congress->query("UPDATE `congresses` SET `congresses`.`status` = 'S' WHERE `congresses`.`id` = '$congressId'")) {
                $this->redirect(array('controller' => 'evaluations', 'action' => 'distributeArticles', $congressId, $congress['Congress']['evaluations_number']));
            }
        endif;
    }

}

?>